<?php
namespace Swiftx\DateTime;
use Swiftx\Base\Object;

/**
 +---------------------------------------------------------------------------------------------------------------
 * 日期类
 +---------------------------------------------------------------------------------------------------------------
 * @author		Hismer <odaytudio@gmail.com>
 * @since		2014-04-29
 * @copyright	Copyright (c) 2014-2014 Swiftx Inc.
 +---------------------------------------------------------------------------------------------------------------
 */
class Date extends Object {
	
	protected $_stamp;	// 戳
	protected $_year;	// 年
	protected $_month;	// 月
	protected $_day;	// 日
	protected $_week;	// 星期
	
	/**
	 +----------------------------------------------------------
	 * 时间戳
	 +----------------------------------------------------------
	 * @return bool
	 +----------------------------------------------------------
	 */
	protected function _getStamp(){
		if(empty($this->_stamp))
			$this->_SyncStamp();
		return $this->_stamp;
	}
	
	/**
	 +----------------------------------------------------------
	 * 时间戳
	 +----------------------------------------------------------
	 * @param int $value
	 +----------------------------------------------------------
	 */
	protected function _setStamp($value){
		$this->_stamp = $value;
		$this->_SyncAttribute();
	}
	
	/**
	 +----------------------------------------------------------
	 * 时间:年
	 +----------------------------------------------------------
	 * @return bool
	 +----------------------------------------------------------
	 */
	protected function _getYear(){
		return $this->_year;
	}
	
	/**
	 +----------------------------------------------------------
	 * 时间:年
	 +----------------------------------------------------------
	 * @param int $value
	 +----------------------------------------------------------
	 */
	protected function _setYear($value){
		$this->_stamp = null;
		$this->_year = $value;
	}

	/**
	 +----------------------------------------------------------
	 * 时间:月
	 +----------------------------------------------------------
	 * @return bool
	 +----------------------------------------------------------
	 */
    protected function _getMonth(){
        return $this->_month < 10 ? '0'.$this->_month:$this->_month;
    }

    /**
	 +----------------------------------------------------------
	 * 时间:月
	 +----------------------------------------------------------
	 * @param int $value
	 +----------------------------------------------------------
	 */
	protected function _setMonth($value){
		$this->_stamp = null;
		$this->_month = $value;
	}

	/**
	 +----------------------------------------------------------
	 * 时间:日
	 +----------------------------------------------------------
	 * @return bool
	 +----------------------------------------------------------
	 */
    protected function _getDay(){
        return $this->_day < 10 ? '0'.$this->_day:$this->_day;
    }


    /**
	 +----------------------------------------------------------
	 * 时间:日
	 +----------------------------------------------------------
	 * @param int $value
	 +----------------------------------------------------------
	 */
	protected function _setDay($value){
		$this->_stamp = null;
		$this->_day = $value;
	}

	/**
	 +----------------------------------------------------------
	 * 时间:星期
	 +----------------------------------------------------------
	 * @return int
	 +----------------------------------------------------------
	 */
	protected function _getWeek(){
		return $this->_week;
	}

	/**
	 +----------------------------------------------------------
	 * 构造方法,时间初始化
	 +----------------------------------------------------------
	 * @param $data	可选，时间的数据
	 +----------------------------------------------------------
	 */
	public function __construct($data = null) {
		if(empty($data)){
			$this->_stamp = \time();
			$this->_SyncAttribute();
			$this->_SyncStamp();
		} else if(is_int($data)){
			$this->_stamp = $data;
			$this->_SyncAttribute();
			$this->_SyncStamp();
		} else if(is_string($data)){
			$this->_stamp = strtotime($data);
			$this->_SyncAttribute();
			$this->_SyncStamp();
		} else if (is_array($data)){
			$this->_stamp = \mktime(0, 0, 0,$data['month'], $data['day'], $data['year']);
			$this->_SyncAttribute();
		} else {
			trigger_error("new Swiftx\\DateTime\\Time() Parameters must be null or int or array", E_USER_ERROR);
		}
	}
	
	/**
	 +----------------------------------------------------------
	 * 构造方法,时间初始化
	 +----------------------------------------------------------
	 * @return string	Y-m-d H:i:s 标准格式输出时间
	 +----------------------------------------------------------
	 */
	public function __toString(){
		return \date('Y-m-d', $this->_stamp);
	}
	
	/**
	 +----------------------------------------------------------
	 * 从时间属性同步到时间戳
	 +----------------------------------------------------------
	 */
	protected function _SyncStamp(){
		$this->_stamp = \mktime(0, 0, 0, $this->_month, $this->_day, $this->_year);
	}
	
	/**
	 +----------------------------------------------------------
	 * 从时间戳同步到时间属性
	 +----------------------------------------------------------
	 */
	protected function _SyncAttribute(){
		$date = \getdate($this->_stamp); 
		$this->_year = $date['year'];
		$this->_month = $date['mon'];
		$this->_day = $date['mday'];
		$this->_week = $date['wday'];
	}

}